import requests
from bs4 import BeautifulSoup
import xlwt


def get_html_content(percent):
    soup = BeautifulSoup(open(file_path + file_name + '.html', encoding='utf-8'),
                         features='html.parser',
                         from_encoding='utf-8')

    f = xlwt.Workbook()
    sheet1 = f.add_sheet('基金概览', cell_overwrite_ok=True)

    sheet1.write(0, 0, "序号", set_style('Times New Roman', 220, True))
    sheet1.write(0, 1, "基金代码", set_style('Times New Roman', 220, True))
    sheet1.write(0, 2, "基金简称", set_style('Times New Roman', 220, True))
    wanted_soup = soup.find_all('tr')
    break_flag = int(len(wanted_soup) * percent)
    start_index = 0
    for idx, tr in enumerate(wanted_soup):
        tds = tr.find_all('td')
        print(idx + 1, tds[2].contents[0].text, tds[3].contents[0].text)
        sheet1.write(idx + 1, 0, idx + 1)
        sheet1.write(idx + 1, 1, tds[2].contents[0].text)
        sheet1.write(idx + 1, 2, tds[3].contents[0].text)
        if start_index > break_flag:
            break
        start_index = start_index + 1

    f.save(save_path + file_name + '.xls')


def set_style(name, height, bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style


bai_fen_bi = 1 / 4
file_name = '债券型_近5年_前四分之一'
file_path = 'D:\\porn\\jij_jin_dir\\债券型\\html\\'
save_path = 'D:\\porn\\jij_jin_dir\\债券型\\excel\\'
get_html_content(bai_fen_bi)
